package com.wz.shortmesg;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.telephony.SmsManager;

import com.wz.bean.SendedMsg;
import com.wz.db.SmsProvider;

/**
 * 发送短信
 */
public class SmsBiz {
	
	private Context context;
	
	public SmsBiz( Context context){
		this.context = context;
	}
	
	/**
	 * 发送单个短息
	 * @param phone
	 * @param msg
	 * @param sentIntent
	 * @param deliveryIntent
	 * @return
	 */
	public static int sendMsg(String phone,String msg,PendingIntent sentIntent,PendingIntent deliveryIntent){
		
		SmsManager smsManager = SmsManager.getDefault();
		ArrayList<String> divideMessage = smsManager.divideMessage(msg);
		for (String content : divideMessage) {
			smsManager.sendTextMessage(phone, null, content, sentIntent, deliveryIntent);
		}
		return divideMessage.size();
	}
	
	/**
	 * 发送多个短信,返回发送的数量
	 * @param phones
	 * @param msg
	 * @param sentIntent
	 * @param deliveryIntent
	 * @return
	 */
     public int sendMsg(List<String> phones,SendedMsg msg,PendingIntent sentIntent,PendingIntent deliveryIntent){
		int result = 0;
		for (String phone : phones) {
			int count = sendMsg(phone, msg.getContent(), sentIntent, deliveryIntent);
			result += count;
		}
		save(msg);
		return result;
	}
	
     
     //因为这里只涉及数据库的增和查，所以这里直接写在业务层下
     //但是如果涉及到的数据库的操作比较多（增删改查，以及按条件查询等），最好在项目的包下建立一个"dao"层
     //或者在"db"下增加一个相关操作的类，然后业务层就可以根据其实例去访问数据库的一些内容
     private void save(SendedMsg sendedMsg) {
         sendedMsg.setDate(new Date());
         ContentValues values=new ContentValues();
         values.put(SendedMsg.COLUMN_DATE,sendedMsg.getDate().getTime());
         values.put(SendedMsg.COLUMN_FESTIVAL_NAME,sendedMsg.getFestivalName());
         values.put(SendedMsg.COLUMN_CONTENT,sendedMsg.getContent());
         values.put(SendedMsg.COLUMN_NAMES,sendedMsg.getNames());
         values.put(SendedMsg.COLUMN_NUMBERS,sendedMsg.getNumbers());

         context.getContentResolver().insert(SmsProvider.URI_SMS_ALL,values);
     }
	
	

}


















